Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxHighlighter: Safely access clipboard on global.navigator #14035

Merged
merged 1 commit into from
Feb 24, 2021

Conversation

kylegach
Copy link
Contributor

@kylegach kylegach commented Feb 24, 2021

Issue: None; discussed in Discord first

if (navigator.clipboard) {
  copyToClipboard = (text: string) => navigator.clipboard.writeText(text);
} else {
  // ...
}

In my admittedly convoluted project (details: #12959 (comment)), I get an error about not being able to access clipboard on undefined, because navigator (expectedly) is not available in the environment in which it is executed.

What I did

Use optional chaining to safely access clipboard on global.navigator

How to test

  • Is this testable with Jest or Chromatic screenshots? No
  • Does this need a new example in the kitchen sink apps? No
  • Does this need an update to the documentation? No

If your answer is yes to any of these, please make sure to include it in your PR.

Questions

A quick search shows that there may be other instances of unsafe global access. Let me know if this PR should attempt to address these, too.

Use optional chaining to safely access `clipboard` on `global.navigator`
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@shilman shilman added bug ui patch:yes Bugfix & documentation PR that need to be picked to main branch labels Feb 24, 2021
@shilman shilman merged commit dd7da20 into storybookjs:next Feb 24, 2021
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label Feb 24, 2021
shilman added a commit that referenced this pull request Feb 24, 2021
SyntaxHighlighter: Safely access clipboard on global.navigator
@kylegach kylegach deleted the patch-1 branch March 2, 2021 19:43
This was referenced Mar 7, 2021
This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants